package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.SetmealDishesMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private SetmealDishesMapper setmealDishesMapper;


    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    public void insert(SetmealDTO setmealDTO) {
        log.info("新增套餐：{}",setmealDTO);
        //数据拷贝进setmeal
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //套餐基础信息添加
        setMealMapper.insert(setmeal);
        //套餐包含菜品信息添加
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        Long id = setmeal.getId();

        if (setmealDishes!=null && setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish ->
                    setmealDish.setDishId(id)
            );


            setmealDishesMapper.insert(setmealDishes);
        }
    }

    /**
     * 分页查询
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setMealMapper.getPage(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 批量删除
     * @param ids
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        log.info("批量删除：{}",ids);
        //删除规定 如果套餐在售卖中，不可删除
        Long enableCount= setMealMapper.getBySetmealEnableCount(ids, StatusConstant.ENABLE);
        if (enableCount>0){
            log.error(MessageConstant.SETMEAL_ON_SALE);
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);

        }
        //批量删除套餐
        setMealMapper.deleteBatch(ids);
    }

    /**
     * 套餐起售、停售
     * @param status
     * @param id
     */
    @Override
    public void startOrClose(Integer status, Long id) {
        log.info("套餐起售、停售：{}，{}",status,id);
        setMealMapper.updateByStatus(status,id);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        log.info("根据id查询套餐:{}",id);
        SetmealVO setmealVO = setMealMapper.getById(id);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        log.info("修改套餐:{}",setmealDTO);
        //1.修改套餐的基本信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setMealMapper.updateById(setmeal);
        //2.修改套餐内的菜品信息

        //获取菜品信息集合
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        Long setmealId = setmealDTO.getId();
        //先删除掉菜品内的信息,在再添加菜品信息
        setmealDishesMapper.deleteById(setmealId);
            setmealDishes.forEach(setmealDish ->
                    setmealDish.setSetmealId(setmealId));
            setmealDishesMapper.insert(setmealDishes);
    }
}
